package tech.bmatch.meituan;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.bmatch.meituan.model.Dishes;
import tech.bmatch.meituan.model.Merchant;
import tech.bmatch.meituan.model.MerchantSearchParam;
import tech.bmatch.meituan.service.MerchantService;
import tech.bmatch.meituan.service.impl.MerchantServiceImpl;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;

/**
 * Application类，各功能的使用
 */

public class Application {
    private static final Logger logger = LoggerFactory.getLogger(Application.class);
    private static MerchantService merchantService = new MerchantServiceImpl();

    public static void main(String[] args) {
        //初始化商家信息
        merchantService.init();
        logger.info("启动应用");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            String command = null;
            try {
                command = bufferedReader.readLine();
            } catch (Exception e) {
                logger.error("", e);
            }
            System.out.println("执行命令：" + command);
            if (command.startsWith("addDishes ")) {
                //执行数据添加
                String content = command.replaceAll("addDishes ", "");
                String[] items = content.split(" ");
                initMerchantDishes(items);
            }
            if (command.startsWith("add ")) {
                String content = command.replaceAll("add ", "");
                String[] items = content.split(" ");
                initMerchant(items);
            }
            if (command.startsWith("search ")) {
                String content = command.replaceAll("search ", "");
                search(content);
            }
        }
    }


    private static void search(String name) {

        MerchantSearchParam param = new MerchantSearchParam();
        param.setName(name);
        param.setLon(120.026208);
        param.setLat(30.279212);
        List<Merchant> merchantList = merchantService.search(param);
        for (Merchant merchant : merchantList) {
            System.out.println("商家名称:" + merchant.getName());
            System.out.println("距离：" + merchant.getDistance());
            System.out.println("-------------------------");
            List<Dishes> dishes = merchant.getDishes();
            if (dishes == null) {
                System.out.println("  还没有菜品。。");
            } else {
                for (Dishes dish : dishes) {
                    System.out.println("  名称：" + dish.getName() + " 价格:" + dish.getPrice() + "元");
                }
            }
            System.out.println("------------------");

        }


    }

    private static void initMerchant(String[] items) {
        Merchant merchant = new Merchant();

        merchant.setId(items[0]);
        merchant.setName(items[1]);
        merchant.setLon(Double.valueOf(items[2]));
        merchant.setLat(Double.valueOf(items[3]));

        merchantService.add(merchant);

    }

    private static void initMerchantDishes(String[] items) {
        Dishes dish = new Dishes();
        dish.setMerchantId(items[0]);
        dish.setName(items[1]);
        dish.setPrice(Double.valueOf(items[2]));
        merchantService.addDishes(dish);
    }

}




    /*private static void init(){
        Merchant merchant = new Merchant();
        merchant.setId("1");
        merchant.setName("杭州麦当劳未来城高教路餐厅");
        merchant.setLon(120.021523);
        merchant.setLat(30.275512);
        merchantService.add(merchant);
        merchant = new Merchant();
        merchant.setId("2");
        merchant.setName("肯德基宅急送（杭州高教DT店)");
        merchant.setLon(120.022002);
        merchant.setLat(30.274293);
        merchantService.add(merchant);
    }*/

